﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Text.RegularExpressions;
using System.IO.Compression;

namespace TvGuide.UI.Controllers
{
    public class BaseController : Controller
    {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            // Gzip the response
            Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
            Response.AppendHeader("Content-encoding", "gzip");
            Response.Cache.VaryByHeaders["Accept-encoding"] = true;
            
            Regex exp = new Regex(@"/(\w+)/schedule/([0-9]+)/([0-9]+)");
            Match match = exp.Match(Request.Url.AbsoluteUri);

            if (match.Success)
            {
                string lang = match.Groups[1].Value;
                int channelId = Int32.Parse(match.Groups[2].Value);
                string id = match.Groups[3].Value;
                TVGuide.DataAccess.tvchanneldbEntities model = new TVGuide.DataAccess.tvchanneldbEntities();
                var channel = model.channels.FirstOrDefault(o => o.channelID == channelId);

                if (channel != null)
                {
                    Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                    Response.StatusDescription = "Moved Permanently";
                    if (Request.Url.Host == "localhost")
                    {
                        Response.RedirectLocation = string.Format("http://localhost:{0}/{1}/schedule/{2}-tv/{3}/{4}", Request.Url.Port, lang, channel.channelName, channelId, id);
                    }
                    else
                    {
                        Response.RedirectLocation = string.Format("http://{0}/{1}/schedule/{2}-tv/{3}/{4}", Request.Url.Host, lang, channel.channelName, channelId, id);
                    }
                    Response.End();

                    filterContext.Result = new RedirectResult(Response.RedirectLocation);
                }
            }

            if (Request.Url.AbsoluteUri.ToLower().StartsWith("http://tvcuyc.am"))
            {
                Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                Response.RedirectLocation = Request.Url.ToString().Replace("http://", "http://www.");
                Response.End();
                filterContext.Result = new RedirectResult(Response.RedirectLocation);
            }

            // Default lang is "arm" so redirect to that lang by default
            if (Request.Url.AbsoluteUri.ToLower() == "http://www.tvcuyc.am")
            {
                Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                Response.RedirectLocation = "http://www.tvcuyc.am/arm/";
                Response.End();
                filterContext.Result = new RedirectResult(Response.RedirectLocation);
            }

            // Redirect old h1 online tv URL to newest URL
            if (Request.Url.AbsoluteUri.ToLower()=="http://www.tvcuyc.am/arm/watch/h1")
            {
                Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
                Response.RedirectLocation = "http://www.tvcuyc.am/arm/Watch/H1-tv-online";
                Response.End();
                filterContext.Result = new RedirectResult(Response.RedirectLocation);
            }

        }
    }
}
